#include "../mqserver/mq_binding.hpp"
#include <gtest/gtest.h>

bitmq::BindingManager::ptr bmp;
class BindingTest : public testing::Environment
{
    virtual void SetUp() override
    {
        bmp = std::make_shared<bitmq::BindingManager>("./data/meta.db");
    }

    virtual void TearDown() override
    {
        // emp->clear();
    }
};
// 插入测试
TEST(binding_test, insert_test)
{
    bmp->bind("e1", "q1", "news.music.#", true);
    bmp->bind("e1", "q2", "news.music.#", true);
    bmp->bind("e1", "q3", "news.music.#", true);
    bmp->bind("e2", "q1", "news.music.pop", true);
    bmp->bind("e2", "q2", "news.sport.football", true);
    bmp->bind("e2", "q3", "news.music.#", true);
    ASSERT_EQ(bmp->size(), 6);
}
// 查询测试
TEST(bingding_test, select_test)
{
    ASSERT_EQ(bmp->exists("e1", "q1"), true);
    ASSERT_EQ(bmp->exists("e2", "q2"), true);
    ASSERT_EQ(bmp->exists("e1", "q2"), true);
    ASSERT_EQ(bmp->exists("e2", "q1"), true);
    bitmq::Binding::ptr bp = bmp->getBinding("e1", "q1");
    ASSERT_NE(bp.get(), nullptr);
    ASSERT_EQ(bp->exchange_name, std::string("e1"));
    ASSERT_EQ(bp->msgqueue_name, std::string("q1"));
    ASSERT_EQ(bp->binding_key, std::string("news.music.#"));
}

TEST(bingding_test, select_exchange_test)
{
    bitmq::MsgQueueBindingMap mqbm = bmp->getExchangeBindings("e1");
    ASSERT_EQ(mqbm.size(), 3);
    ASSERT_NE(mqbm.find("q1"), mqbm.end());
    ASSERT_NE(mqbm.find("q2"), mqbm.end());
}

// 删除测试
TEST(bingding_test, remove_queue_test)
{
    bmp->removeMsgQueueBindings("q1");
    ASSERT_EQ(bmp->exists("e1", "q1"), false);
    ASSERT_EQ(bmp->exists("e2", "q1"), false);
}
TEST(bingding_test, remove_exchange_test)
{
    bmp->removeExchangeBindings("e1");
    ASSERT_EQ(bmp->exists("e1", "q1"), false);
    ASSERT_EQ(bmp->exists("e1", "q2"), false);
    ASSERT_EQ(bmp->exists("e1", "q3"), false);
}
TEST(bingding_test, remove_signal_test)
{
    ASSERT_EQ(bmp->exists("e2", "q2"), true);
    bmp->unBind("e2", "q2");
    ASSERT_EQ(bmp->exists("e2", "q2"), false);
}

int main(int argc, char *argv[])
{
    testing::InitGoogleTest(&argc, argv);
    testing::AddGlobalTestEnvironment(new BindingTest);
    int t = RUN_ALL_TESTS();
    return 0;
}